<?php

class Adept_Renderer_ListBox extends Adept_Renderer_Composite
{

    public function renderBegin($component)
    {
        $attributes = array(
            'id' => $component->getId(),
            'name' => $component->getName(),
            'style' => $component->getStyle(),
            'class' => $component->getClass(),
            'size' => $component->getSize(),
            'multiple' => $component->isMultiple() ? 'true' : null,
        );
        
        $attributes = array_merge($attributes, $component->getBrowserEvents());
        
        $writer = $component->getWriter();
        $writer->writeHtmlTag('select', $attributes);
    }
    
    public function renderChildren($component)
    {
        $writer = $component->getWriter();
        foreach ($component->getItemsArray() as $item) {
            $selected = null;
            if ($component->isItemSelected($item->getValue())) {
                $selected = 'true';
            }
            $writer->writeHtmlTag('option', array('value' => $item->getValue(), 'selected' => $selected));    
            $writer->write($item->getTitle());
            $writer->write("</option>\n");
        }
    }

    public function renderEnd($component)
    {
        $writer = $component->getWriter();
        $writer->write("</select>\n");
    }

}